const https = require('https'),
    fs = require('fs'),
    http = require('http'),
    axios = require('axios'),
    chreeio = require('cheerio'),
    Iconv = require('iconv-lite'),
    puppeteer = require('puppeteer'),
    request = require('request');

const rootUrl = 'http://www.imomoe.ai/search.asp?page=1&searchword=tv&searchtype=-1';

const { con } = require('../models/common');
// 日志的输出 0最多开发使用,1最少运行使用
let leve = 0;

class Result {
    constructor() {
        this.id = ' ';//id主键
        this.url = ' ';//目标地址
        this.img = ' ';//图片
        this.name = ' ';//名字
        this.alias = ' ';//别名
        this.type = ' ';//类型+集数
        this.synopsis = ' ';//简介

        this.years = ' ';//年代
        this.region = ' ';// 地区
        this.Indexes = ' '; // 索引
        this.label = ' ';// 标签

        this.num = ' ';//集数
        // this.addnum = ' ';//地址数量
        this.introduction = ' ';// 简介
        this.iteminfo = new Array();
        // 资源路径
        // 第几集的要存到一个表里面把
    }
}

class Vedio {
    constructor() {
        // 所属动漫id
        this.id = ' ';
        // 第几集
        this.num = ' ';
        // 第几个播放链接
        // this.addnum = ' ';
        // 播放地址
        this.url = ' ';
    }
}
let detailurlList = new Array();
let itemList = new Array();
// 获取页数
function getNum(rootUrl) {
    return new Promise((resolve, reject) => {
        request({ encoding: null, url: rootUrl },
            function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    const html = Iconv.decode(body, 'gb2312');
                    let $ = chreeio.load(html);
                    let pageSum = $('#contrainer>.tame>span>em').text();
                    resolve(pageSum);
                }
            });
    })
}

// 定时器操作，防止IP被封
function Timer() {
    return new Promise((resolve, reject) => {
        setTimeout(resolve, 1000);
    })
}
function getIteminfo(url, num) {
    return new Promise((resolve, reject) => {
        request({ encoding: null, url: url },
            async function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    const html = Iconv.decode(body, 'gb2312');
                    let $ = chreeio.load(html);

                    let result = new Result();
                    let url = $('.spay').find('a').attr('href');
                    let info = $('.alex>p').eq(0).text();
                    // url = url.slice(6, 10);
                    result.url = url;
                    result.id = parseInt(url.slice(6, 10));
                    result.alias = info;
                    result.img = $('.tpic>img').attr('src');
                    result.synopsis = $('.alex>p').eq(1).text();
                    result.name = $('.tpic>img').attr('alt');
                    result.region = $('.alex').find('span').eq(0).find('a').text();
                    let type = $('.alex').find('span').eq(1).find('a');
                    let typetext = '';
                    for (let i = 0; i < type.length; i++) {
                        typetext += `${$(type[i]).text()} `;
                    }
                    result.type = typetext;
                    result.years = $('.alex').find('span').eq(2).find('a').text();
                    result.label = $('.alex').find('span').eq(3).find('a').first().text();
                    result.Indexes = $('.alex').find('span').eq(4).find('a').text().slice(1, 3);
                    result.num = num;
                    result.introduction = $('body').find('.info').text();

                    itemList.push(result);
                    try {
                        let sql = `select * from animation where id = ${result.id}`;
                        let [rows] = await con.execute(sql);
                        if (rows.length >= 1) {
                            // let del = `delete from animation where id = ${result.id}`;
                            // let [rows] = await con.execute(del);
                            // let ins = `insert into animation values('${result.id}','${result.url}','${result.img}','${result.name}','${result.alias}','${result.type}','${result.synopsis}','${result.years}','${result.region}','${result.Indexes}','${result.label}','${result.num}','${result.introduction}',0)`;
                            // let [rows2] = await con.execute(ins);
                            let updata = `update animation set synopsis = '${result.synopsis}' where id = ${result.id}`;
                            let updata2 = `update animation set num = ${result.num} where id = ${result.id}`;
                            await con.execute(updata);
                            await con.execute(updata2);
                            if (leve == 0) {
                                console.log(result.name + '添加成功');
                            }
                        } else {
                            console.log(result);
                            let ins = `insert into animation values('${result.id}','${result.url}','${result.img}','${result.name}','${result.alias}','${result.type}','${result.synopsis}','${result.years}','${result.region}','${result.Indexes}','${result.label}','${result.num}','${result.introduction}',0)`;
                            let [rows2] = await con.execute(ins);
                            if (leve == 0) {
                                console.log(result.name + '添加成功');
                            }
                        }
                    } catch (error) {
                        console.log(error)
                    }
                }
                resolve();
            }
        )
    })
}
function getresultNum(url) {
    return new Promise((resolve, reject) => {
        request({ encoding: null, url: url },
            function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    const html = Iconv.decode(body, 'gb2312');
                    let $ = chreeio.load(html);
                    let urlList = $('#play_0>ul').find('li');
                    resolve(urlList.length);
                }
            });
    })
}
// 此函数传入一页url用于得到该页面上的番剧信息
function getItem(url) {
    return new Promise((resolve, reject) => {
        request({ encoding: null, url: url },
            function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    const html = Iconv.decode(body, 'gb2312');
                    let $ = chreeio.load(html);
                    // 链接部分
                    let urlList = $('.fire>.pics>ul').find('li>a');

                    // 在循环中进行爬取到的数据的相关操作
                    // 将页面的url放入队列中
                    // 爬取然后新建一个函数去爬取详情页的信息，传入目标番剧的url在函数中拼接成正确的url
                    // 使用js的队列相关的操作，如果队列不为空则爬取，为空结束当前页面去爬取下一页
                    for (let i = 0; i < urlList.length; i++) {
                        let url = $(urlList[i]).attr('href');
                        detailurlList.push(url);
                    }
                    resolve();
                }
            });
    })
}
// getIteminfo(rootUrl);
async function getVideoUrl(url) {
    // return new Promise((resolve, reject) => {
    //创建一个Browser浏览器实例，并设置相关参数
    const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
    //创建一个Page实例
    const page = await browser.newPage();
    //console.log(url);
    await page.goto(url);
    const video = await page.$eval('#play2', e => e.src);
    // console.log(video);
    browser.close();
    return video;
}
// 将此段代码封装为函数传入不同的url以此达到爬数据的功效
async function Test() {
    console.log('开始');
    console.log('页面爬取完毕，开始爬取各个动漫的详细内容。。。');
        url = `http://www.imomoe.ai/view/3215.html`;
        console.log(url);
        let num = await getresultNum(url);
        await getIteminfo(url, num);
        await Timer();
    console.log('结束');
}
Test();
module.exports = { Test };
